{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import cv2\n",
    "import pylab\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "import pickle\n",
    "%matplotlib inline\n",
    "pylab.rcParams['figure.figsize'] = (24.0, 24.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "json_dir = 'demo_datasets/annotations/'\n",
    "image_dir = 'demo_datasets/images/'\n",
    "resPth = 'bbox_predict.pkl'\n",
    "demo_cats = [{'id': 1, 'name': 'plane', 'supercategory': 'plane'}, \n",
    "             {'id': 2, 'name': 'baseball-diamond', 'supercategory': 'baseball-diamond'},\n",
    "             {'id': 3, 'name': 'bridge', 'supercategory': 'bridge'}, \n",
    "             {'id': 4, 'name': 'ground-track-field', 'supercategory': 'ground-track-field'}, \n",
    "             {'id': 5, 'name': 'small-vehicle', 'supercategory': 'small-vehicle'}, \n",
    "             {'id': 6, 'name': 'large-vehicle', 'supercategory': 'large-vehicle'},\n",
    "             {'id': 7, 'name': 'ship', 'supercategory': 'ship'}, \n",
    "             {'id': 8, 'name': 'tennis-court', 'supercategory': 'tennis-court'}, \n",
    "             {'id': 9, 'name': 'basketball-court', 'supercategory': 'basketball-court'},\n",
    "             {'id': 10, 'name': 'storage-tank', 'supercategory': 'storage-tank'}, \n",
    "             {'id': 11, 'name': 'soccer-ball-field', 'supercategory': 'soccer-ball-field'}, \n",
    "             {'id': 12, 'name': 'roundabout', 'supercategory': 'roundabout'}, \n",
    "             {'id': 13, 'name': 'harbor', 'supercategory': 'harbor'}, \n",
    "             {'id': 14, 'name': 'swimming-pool', 'supercategory': 'swimming-pool'},\n",
    "             {'id': 15, 'name': 'helicopter', 'supercategory': 'helicopter'}]\n",
    "\n",
    "img_names = []\n",
    "for file_name in os.listdir(image_dir):\n",
    "    if 'png' in file_name:\n",
    "        img_names.append(file_name)\n",
    "demo_json = {\n",
    "    'images': [],\n",
    "    'annotations': [],\n",
    "    'categories': demo_cats\n",
    "}\n",
    "for i, name in enumerate(img_names):\n",
    "    img_info = {\n",
    "        'file_name': name,\n",
    "        'id': i + 1,\n",
    "        'width': 1024,\n",
    "        'height': 1024\n",
    "    }\n",
    "    demo_json['images'].append(img_info)\n",
    "f = open(json_dir + 'demo.json', 'w')\n",
    "json.dump(demo_json, f)\n",
    "print(len(demo_json['images']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "demo_json = json.load(open(json_dir + 'demo.json'))\n",
    "results = pickle.load(open(resPth, 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_thr = 0.3\n",
    "catid2label = {info['id']: info['name'] for info in demo_json['categories']}\n",
    "random_pic_id = np.random.randint(0, len(results), 1)[0]\n",
    "random_pic_id = 4\n",
    "img_name = demo_json['images'][random_pic_id]['file_name']\n",
    "img_path = image_dir + img_name\n",
    "img = cv2.imread(img_path)\n",
    "img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "result = results[random_pic_id]\n",
    "for label in range(len(result)):\n",
    "    bboxes = result[label]\n",
    "    for i in range(bboxes.shape[0]):\n",
    "        convex_points = np.array(bboxes[i][:18]) \n",
    "        rbbox = np.array(bboxes[i][18:-1])\n",
    "        score = str(round(bboxes[i][-1], 2))\n",
    "        if float(score) < vis_thr:\n",
    "            continue\n",
    "        label_score = catid2label[label + 1] + ':' + score\n",
    "        cv2.putText(img, label_score, (int(rbbox[0]), int(rbbox[1]) - 10),\n",
    "                    cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1)\n",
    "        for p in convex_points.reshape(-1, 2):\n",
    "            cv2.circle(img, (int(p[0]), int(p[1])), 3, (255, 0, 0), -1)\n",
    "        cv2.polylines(img, rbbox.reshape(1, -1, 2).astype(np.int32), True, (255, 0, 0))\n",
    "\n",
    "plt.imshow(img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mmdet-dota-vfn",
   "language": "python",
   "name": "mmdet-dota-vfn"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
